package com.cdtu.controller;

import com.cdtu.entity.PersonalPlan;
import com.cdtu.entity.RegularApply;
import com.cdtu.entity.SysEmployee;
import com.cdtu.service.RegularApplyService;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.SysSalaryService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/apply")
//解决跨域问题
@CrossOrigin
public class RegularApplyController extends BaseController {
    @Autowired
    RegularApplyService regularApplyService;
    @Autowired
    SysEmployeeService sysEmployeeService;

    @RequestMapping("/list")
    public ModelAndView empList(ModelAndView modelAndView) {

        List<RegularApply> regularApplies = regularApplyService.list();
        List<SysEmployee> sysEmployeeList = sysEmployeeService.list();
        //返回数据
        modelAndView.addObject("regularApplies", regularApplies);
        modelAndView.addObject("sysEmployeeList", sysEmployeeList);
        //2.跳转页面;
        modelAndView.setViewName("train/apply/applyList");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysEmp(HttpSession session, ModelAndView modelAndView) {
        SysEmployee user = (SysEmployee) session.getAttribute("USER");
        if (user.getFormal() == 1) {
            modelAndView.addObject("message", "你已转正，无法提交转正申请");
        }
        modelAndView.setViewName("train/apply/addApply");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/add")
    public ModelAndView addEmpUI(RegularApply regularApply,ModelAndView modelAndView) {
        regularApply.setApplyTime(new Date());
        regularApply.setCreactTime(new Date());
        regularApply.setCheckResult(0);
        regularApplyService.save(regularApply);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/apply/list");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/delete/{applyId}")
    public ModelAndView delete(@PathVariable("applyId") Long applyId, ModelAndView modelAndView) {
        boolean remove = regularApplyService.removeById(applyId);
        modelAndView.setViewName("redirect:/apply/list");
        return modelAndView;
    }

    @RequestMapping("/check/{applyId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("applyId") Long applyId) {
        RegularApply regularApply = regularApplyService.getById(applyId);
        SysEmployee sysEmployee = sysEmployeeService.getById(regularApply.getApplyEmpId());
        System.out.println(regularApply);
        System.out.println(sysEmployee);
        //返回数据
        modelAndView.addObject("regularApply", regularApply);
        modelAndView.addObject("sysEmployeeName", sysEmployee.getName());
        //跳转页面;
        modelAndView.setViewName("train/apply/check");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView,RegularApply regularApply) {
        if (regularApply.getCheckResult()==1){
            SysEmployee sysEmployee=new SysEmployee();
            sysEmployee.setId(regularApply.getApplyId());
            sysEmployee.setFormal(1);
            System.out.println(sysEmployee);
            //设置申请人是否转正
            sysEmployeeService.updateById(sysEmployee);
        }
        regularApply.setCheckTime(new Date());
        System.out.println(regularApply);
        regularApplyService.updateById(regularApply);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/apply/list");
        return modelAndView;
    }

}
